package com.study.service.impl;

import com.study.dao.AccountDao;
import com.study.pojo.Account;
import com.study.service.TransferService;

public class TransferServiceImpl implements TransferService {

    // 通过多态的方式引入数据持久层，可以通过 IOC 的方式优化
    //private AccountDao accountDao = new AccountDaoImpl();
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        // 查询两个用户的账户信息
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        //对账户信息转账的金额进行处理
        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);

        //更新两个账户的金额
        accountDao.updateAccountByCardNo(to);
        //为了测试事务回滚
        int num = 1/0;
        accountDao.updateAccountByCardNo(from);
    }
}
